package com.baiytfp.hf.HFDataExchange.action.controller.service;

import com.baiytfp.hf.HFDataExchange.action.service.FuncService;
import com.baiytfp.hf.HFDataExchange.action.service.TreeService;
import com.baiytfp.hf.HFDataExchange.common.model.state.TreeType;
import com.baiytfp.hf.HFDataExchange.common.model.table.Func;
import com.baiytfp.hf.HFDataExchange.common.model.table.Tree;
import com.baiytfp.hf.baiytfp.common.util.ResponseUtil;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import com.baiytfp.hf.baiytfp.hfutils.HFRequest;
import com.baiytfp.hf.baiytfp.hfutils.HFResponse;
import com.baiytfp.hf.baiytfp.hfutils.HFWeb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/service/func")
public class FuncSController {
	
	@Autowired
	public FuncService funcService;

	@Autowired
	public TreeService treeService;
	
	//获取树
	@RequestMapping("treelist")
	public ModelAndView treelist(HttpServletRequest request,HttpServletResponse response)throws Exception{
		int treeType= TreeType.MANAGE_FUNC.getKey();
		
		List<Map> list=null;
		
		String parentTreeCode = HFRequest.getIstance().getString(request, "parent_tree_code","");
		
		list=funcService.getTree(String.valueOf(treeType), parentTreeCode);
		
		HFResponse.getIstance().returnResult(response, list);
		return null;
	}
		
	//添加节点
	@RequestMapping("/tnodeadd")
	public ModelAndView tnodeadd(HttpServletRequest request,HttpServletResponse response)throws Exception{
		int treeType=TreeType.MANAGE_FUNC.getKey();
		Map resultMap = new HashMap(16);
		
		String parentTreeCode = HFRequest.getIstance().getString(request, "parent_tree_code","-1");
		
		Tree treeVO = HFWeb.getIstance().toBean(request, Tree.class);
		
		treeVO.setTree_type(treeType);
		treeVO.setId(HFDBHelper.getIDGenerator());
		treeVO.setChildren_number(0);//新结点，没有子结点
		treeVO.setIs_open(1);
		treeVO.setParent_tree_code(parentTreeCode);
		treeVO.setTree_code(treeService.getNewSeqCode(parentTreeCode, String.valueOf(treeType)));//取得流水号
		treeVO.setNode_order(treeService.setChildNodeOrder(parentTreeCode, 0, "000", String.valueOf(treeType)));// 对新父结点的孩子（非此结点）进行序号排序
	
		//设置个性附加属性内容
		Func func= HFWeb.getIstance().toBean(request, Func.class);
		func.setTree_id(treeVO.getId());
		
		//设置树结点名
		treeVO.setNode_name(func.getFunc_name());
		

		
		funcService.addFunc(treeVO, func);

		resultMap.put("treeVO", treeVO);
		resultMap.put("regionUnitVO", func);

		HFResponse.getIstance().returnResult(response, resultMap);
		return null;
	}
		
	//编辑节点
	@RequestMapping("/tnodeedit")
	public ModelAndView tnodeedit(HttpServletRequest request,HttpServletResponse response)throws Exception {
		
		int treeType = TreeType.MANAGE_FUNC.getKey();
		String treeCode = HFRequest.getIstance().getString(request,"tree_code", "");
		
		Map rs = funcService.updateFunc(request.getParameterMap(),treeType,treeCode);
		
		Map resultMap = new HashMap();
		resultMap.put("treeVO", rs.get("treeVO"));
		resultMap.put("funcVO", rs.get("funcVO"));

		HFResponse.getIstance().returnResult(response, resultMap);
		return null;
	}
	
	// 删除一个菜单结点
	@RequestMapping("/tnodedel")
	public ModelAndView tnodedel(HttpServletRequest request,HttpServletResponse response) throws Exception {

		String treeCode = HFRequest.getIstance().getString(request,"treeCode", "");
		funcService.delFuncByTreeCode(treeCode);
		HFResponse.getIstance().returnSuccess(response);
		return null;
	}
	
}
